package com.qianbao.qianbaobusiness.help;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.qianbao.qianbaobusiness.database.CityDao;
import com.qianbao.qianbaobusiness.database.DistrictDao;
import com.qianbao.qianbaobusiness.database.PcdDatabase;
import com.qianbao.qianbaobusiness.database.ProvinceDao;
import com.qianbao.qianbaobusiness.json.PcdBean;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: Routee
 * @date 2018/6/26
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class PcdDialogHelper {
    Context mContext;
    private ArrayList<String>                       mList1 = new ArrayList<>();
    private ArrayList<ArrayList<String>>            mList2 = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> mList3 = new ArrayList<>();
    private boolean                                           initialed;
    private DistrictSelectedListener                          mListener;
    private List<PcdBean.ProvincesBean>                       mProvince;
    private List<ArrayList<PcdBean.CitysBean>>                mCity;
    private List<ArrayList<ArrayList<PcdBean.DistrictsBean>>> mDistrict;

    public PcdDialogHelper(Context context, DistrictSelectedListener listener) {
        mContext = context;
        initData();
        mListener = listener;
    }

    private synchronized void initData() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                ProvinceDao provinceDao = PcdDatabase.getInstance(Global.getContext()).provinceDao();
                CityDao cityDao = PcdDatabase.getInstance(Global.getContext()).cityDao();
                DistrictDao districtDao = PcdDatabase.getInstance(Global.getContext()).districtDao();

                mProvince = provinceDao.getAll();
                mCity = new ArrayList<>();
                mDistrict = new ArrayList<>();
                for (PcdBean.ProvincesBean bean : mProvince) {
                    mList1.add(bean.getName());
                    ArrayList<PcdBean.CitysBean> citysBeans = new ArrayList<>();
                    ArrayList<String> cityList = new ArrayList<>();
                    ArrayList<ArrayList<PcdBean.DistrictsBean>> districtBeans = new ArrayList<>();
                    ArrayList<ArrayList<String>> districtList = new ArrayList<>();
                    List<PcdBean.CitysBean> citys = cityDao.queryCitys(bean.getCode());
                    for (PcdBean.CitysBean city : citys) {
                        citysBeans.add(city);
                        cityList.add(city.getName());
                        List<PcdBean.DistrictsBean> districts = districtDao.queryDistricts(city.getCode());
                        ArrayList<PcdBean.DistrictsBean> districtsBeanList = new ArrayList<>();
                        ArrayList<String> arreaNames = new ArrayList<>();
                        for (PcdBean.DistrictsBean district : districts) {
                            districtsBeanList.add(district);
                            arreaNames.add(district.getName());
                        }
                        districtBeans.add(districtsBeanList);
                        districtList.add(arreaNames);
                    }
                    mCity.add(citysBeans);
                    mList2.add(cityList);
                    mDistrict.add(districtBeans);
                    mList3.add(districtList);
                }
                initialed = true;
            }
        });
    }

    public void createDialog() {
        if (!initialed) {
            ToastUtils.showLongToast("数据初始化中，请稍后...");
            return;
        }
        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int position1, int position2, int position3, View v) {
                if (mListener != null) {
                    mListener.itemSelected(mProvince.get(position1),
                            mCity.get(position1).get(position2),
                            mDistrict.get(position1).get(position2).get(position3));
                }
            }
        })

                                              .setTitleText("城市选择")
                                              .setDividerColor(Color.BLACK)
                                              .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                                              .setContentTextSize(20)
                                              .build();

        pvOptions.setPicker(mList1, mList2, mList3);//添加数据源
        pvOptions.show();
    }

    public interface DistrictSelectedListener {
        void itemSelected(PcdBean.ProvincesBean province, PcdBean.CitysBean city, PcdBean.DistrictsBean district);
    }
}
